<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>内容管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}"
          href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" href="../../../static/layuimini/css/public.css"
          media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" lay-filter="tableToolForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    搜索类型
                </div>
                <div class="layui-inline">
                    <select id="searchType" name="searchType" lay-filter="contentSearchType">
                        <option value="1">文章标题</option>
                        <option value="2">作者名称</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input id="searchStr" class="layui-input" name="searchStr" type="text" placeholder="输入关键字">
                </div>
                <div class="layui-inline">
                    文章类别
                </div>
                <div class="layui-inline">
                    <select id="contentType" name="contentType" lay-filter="contentType">
                        <option value="">全部文章</option>
                        <option th:each="contentType:${contentTypeList}" th:value="${contentType.typeId}"
                                th:text="${contentType.typeName}" value="1">文章类别
                        </option>
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="btn-search">
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn-reload">
                        <i class="layui-icon">&#xe666;</i>清空搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" layuimini-href="/manage/content/content-edit.html"
                            target="_newtab" id="btn-add11">
                        <i class="layui-icon layui-icon-addition"></i>新建文章
                    </button>
                    <button class="layui-btn layui-btn-danger" id="btn-delete">
                        <i class="layui-icon">&#xe640;</i>删除
                    </button>
                    <button class="layui-btn layui-btn-warm" id="btn-baiduSubmit">
                        <i class="layui-icon">&#xe64c;</i>百度链接提交
                    </button>
                </div>
            </div>
        </form>

        <table class="layui-hide" id="contentTable" lay-filter="currentTableFilter"></table>

    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" src="../../../static/layuimini/lib/layui-v2.5.5/layui.js"
        charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=2.0.0}" src="../../static/layuimini/js/lay-config.js?v=2.0.0"
        charset="utf-8"></script>
<script th:inline="text">
    layui.use(['form', 'table', 'miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            miniTab = layui.miniTab,
            table = layui.table;

        miniTab.render({});
        var contentTable = table.render({
            elem: '#contentTable',
            url: '[[@{/}]]manage/content/list',
            cols: [
                [
                    {type: "checkbox", minWidth: 50},
                    {field: 'contentId', minWidth: 80, title: 'ID'},
                    {field: 'contentTitle', minWidth: 120, title: '文章标题'},
                    {field: 'userShowName', minWidth: 100, title: '作者'},
                    {field: 'contentTypeName', minWidth: 100, title: '类别'},
                    {
                        field: 'status', minWidth: 80, align: 'center', title: '状态', templet: function (d) {
                            return d.status == 1 ? '<span style="color: #5FB878">已发布</span>' : '<span style="color: #dddddd">稿件</span>';
                        }
                    },
                    {
                        field: 'createTime', minWidth: 120, title: '创建时间', templet: function (d) {
                            return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss');
                        }
                    },
                    {
                        title: '操作', minWidth: 150, align: "center", templet: function (d) {
                            var btn = '';
                            btn = '<button class="layui-btn layui-btn-xs  layui-btn-radius" layuimini-href="/manage/content/content-edit.html?contentId='+d.contentId
                                +'" target="_newtab"><i class="layui-icon layui-icon-edit"></i>编辑</button>' +
                                '<button class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>';
                            return btn;
                        }
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        form.on('submit(tableToolForm)', function (data) {
            return false;
        });

        //搜索
        $('#btn-search').click(function () {
            var searchType = $('#searchType').val();
            var searchStr = $('#searchStr').val();
            var contentType = $('#contentType').val();

            table.reload('contentTable', {
                where: { //设定异步数据接口的额外参数，任意设
                    searchType: searchType
                    , searchStr: searchStr
                    , contentType: contentType
                    , type: 1
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
                , done: function () {
                    this.where = {};//清空查询参数
                }
            }); //只重载数据
        });

        //新增
        $('#btn-add').click(function () {
            layer.open({
                type: 2
                , title: '新增用户'
                , content: '[[@{/}]]manage/user/user-edit.html'
                , resize: false
                , area: ['500px', '487px']
                , end: function () {
                    location.reload();
                }
            });
        });

        //清空搜索
        $('#btn-reload').click(function () {
            $('#searchStr').val('');
            table.reload('contentTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); //只重载数据
        });

        //批量删除
        $('#btn-delete').click(function () {
            var dataList = table.checkStatus('contentTable').data;
            var ids = [];
            $(dataList).each(function () {

                ids.push(this.contentId);

            });
            console.log(ids);
            layer.confirm('真的删除选中文章么', function (index) {
                layer.load(2);
                $.ajax({
                    type: 'get',
                    url: '[[@{/}]]manage/content/delete',
                    data: {contentIds: ids},
                    traditional: true,//传统方式传递数组
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                        if (result.success) {
                            layer.msg("操作成功", {shift: -1, time: 500}, function () {
                                table.reload('contentTable');
                            });

                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        layer.msg(textStatus);
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    }
                });
            });

        });

        //百度链接提交
        $('#btn-baiduSubmit').click(function () {
            var dataList = table.checkStatus('contentTable').data;
            var ids = [];
            $(dataList).each(function () {
                if (this.status==1){
                    ids.push(this.contentId);
                }
            });
            console.log(ids);
            if (ids.length==0){
                layer.msg("没有选中已发布的文章");
                return;
            }

            layer.load(2);
            $.ajax({
                type: 'post',
                url: '[[@{/}]]manage/content/baiduSubmit',
                data: {contentIds: ids},
                traditional: true,//传统方式传递数组
                dataType: 'json',
                success: function (result) {
                    console.log(result);
                    if (result.success) {
                        layer.confirm("提交结果:<br>"+result.data,function (index) {
                            layer.close(index);
                        });
                    } else {
                        layer.msg(result.msg);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    layer.msg(textStatus);
                },
                complete: function () {
                    layer.closeAll('loading');
                }
            });


        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //新选项卡
            } else if (obj.event === 'delete') {

                layer.confirm('真的删除么', function (index) {
                    layer.load(2);
                    $.ajax({
                        type: 'get',
                        url: '[[@{/}]]manage/content/delete',
                        data: {contentId: data.contentId},
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success) {
                                layer.msg("操作成功");
                                obj.del();
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });

                    // layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>